<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var networkDelayTime = function (times, n, k) {
      const arr = new Array(n + 1).fill(-1)
      arr[k] = 0
      for (let i = 0; i < n; i++) {
        for (const [a, b, time] of times) {
          if (arr[a] !== -1) {
            arr[b] = arr[b] === -1 ? arr[a] + time : Math.min(arr[b], arr[a] + time)
          }
        }
      }
      if (arr.lastIndexOf(-1) > 0) {
        return -1
      }
      return Math.max(...arr)
    }
    console.log(networkDelayTime([[2, 1, 1], [2, 3, 1], [3, 4, 1]], 4, 2)); 
  </script>

</body>

</html>